<?php
require_once (__DIR__ . "/../../models/Users.php");
require_once (__DIR__ . "/../../models/OrderItems.php");
require_once (__DIR__ . "/../../models/Orders.php");
require_once (__DIR__ . "/../../models/Keywords.php");
require_once (__DIR__ . "/../../models/Products.php");

if(isset($_POST['id']))
{
	$row = $_POST;
	$response = OrderItems::Validate($row);
	if ($response === true)
		$response = OrderItems::Update($row);
	if($response === true)
		header("Location: $rootUrl/../W/OrderItems/index");
}else {
	$row = OrderItems::Get($_REQUEST['id']);
}

?>
				<?php if(isset($response)): ?>
					<dl class="dl-horizontal error">
						<?php foreach ($response as $key => $value): ?>
							<dt><?php echo $key?></dt>
							<dd><?php echo $value?></dd>
						<?php endforeach; ?>						
					</dl>
				<?php endif; ?>
				<form class="form-horizontal" action="" method="post">
					<input type="hidden" name="id" value="<?php echo $row['id']?>" />
					<div class="control-group">
						<label class="control-label" for="productId">Product Id:</label>
						<div class="controls">
<?php $results = Products::GetAll(); 
							echo '<select name="productId">';
	  while($row2 = $results->fetch_assoc()): 
		  if ($row[productId]==$row2[id])
				echo '<option value="'.$row2['id'].'" selected="selected">'.$row2['name'].'</option>';							  	
		  else
				echo '<option value="'.$row2['id'].'" >'.$row2['name'].'</option>';					
	  endwhile;	
	   						echo '</select>';
						?> 
						</div>
					</div>
					<div class="control-group">
						<label class="control-label" for="orderId">Order Id:</label>
						<div class="controls">
<?php $results = Orders::GetAll(); 
							echo '<select name="orderId">';
	  while($row2 = $results->fetch_assoc()):
		  if ($row[orderId]==$row2[id])
				echo '<option value="'.$row2['id'].'" selected="selected">Id: '.$row2['id'].' Created At: '.$row2['created_at'].'</option>';	  	
			else
				echo '<option value="'.$row2['id'].'" >Id: '.$row2['id'].' Created At: '.$row2['created_at'].'</option>';					
	  endwhile;	
	   						echo '</select>';
						?> 
						</div>
					</div>
					<div class="control-group">
						<label class="control-label" for="quantity">Quantity:</label>
						<div class="controls">
							<input type="number" name="quantity" id="quantity" value="<?php echo $row['quantity']; ?>" 
class="<?php echo isset($response['quantity']) ? 'error' : '' ?>"
								/>
								<?php if(isset($response['quantity'])): ?>
									<span class="error"><?php echo $response['quantity']?></span>
								<?php endif; ?>
						</div>
					</div>
					<div class="control-group">
						<div class="controls">
							<input type="submit" value="Save" class="btn btn-primary" />
						</div>
					</div>
			
				</form>